package xyz.zhouzhaodong.crm.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.transaction.annotation.Transactional;
import xyz.zhouzhaodong.crm.dto.LeaveAbsenceDto;
import xyz.zhouzhaodong.crm.entity.LeaveAbsence;
import xyz.zhouzhaodong.crm.entity.LeaveAbsence;
import xyz.zhouzhaodong.crm.mapper.LeaveAbsenceMapper;
import xyz.zhouzhaodong.crm.mapper.LeaveAbsenceMapper;
import xyz.zhouzhaodong.crm.service.ILeaveAbsenceService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import xyz.zhouzhaodong.crm.util.FileUploadUtil;
import xyz.zhouzhaodong.crm.util.RestMessage;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * <p>
 * 请假 服务实现类
 * </p>
 *
 * @author zhouzhaodong
 * @since 2020-06-21
 */
@Service
public class LeaveAbsenceServiceImpl extends ServiceImpl<LeaveAbsenceMapper, LeaveAbsence> implements ILeaveAbsenceService {
    
    @Resource
    LeaveAbsenceMapper leaveAbsenceMapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public RestMessage addLeaveAbsence(LeaveAbsence leaveAbsence) {
        try {
            // 新增
            String id = UUID.randomUUID().toString().replaceAll("-", "");
            leaveAbsence.setId(id);
            leaveAbsence.setCreateUser(leaveAbsence.getStaffId());
            leaveAbsence.setCreateTime(new Date());
            leaveAbsenceMapper.insert(leaveAbsence);
            return RestMessage.success(id);
        } catch (Exception e) {
            return RestMessage.failed(e.getMessage());
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public RestMessage deleteLeaveAbsence(LeaveAbsence leaveAbsence) {
        try {
            LeaveAbsence r = new LeaveAbsence();
            r.setId(leaveAbsence.getId());
            r.setFlag(1);
            r.setUpdateUser(leaveAbsence.getStaffId());
            r.setUpdateTime(new Date());
            leaveAbsenceMapper.updateById(r);
            return RestMessage.success();
        } catch (Exception e) {
            return RestMessage.failed(e.getMessage());
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public RestMessage updateLeaveAbsence(LeaveAbsence leaveAbsence) {
        try {
            leaveAbsence.setUpdateUser(leaveAbsence.getStaffId());
            leaveAbsence.setUpdateTime(new Date());
            leaveAbsence.setStaffId(null);
            leaveAbsenceMapper.updateById(leaveAbsence);
            return RestMessage.success();
        } catch (Exception e) {
            return RestMessage.failed(e.getMessage());
        }
    }

    @Override
    public RestMessage selectLeaveAbsence(LeaveAbsenceDto leaveAbsenceDto) {
        if(leaveAbsenceDto.getPageNo() == 0 && leaveAbsenceDto.getPageSize() == 0){
            List<LeaveAbsenceDto> list = leaveAbsenceMapper.selectLeaveAbsence(leaveAbsenceDto);
            for(LeaveAbsenceDto r : list){
                String imgAddress = r.getImgAddress();
                String base64 = FileUploadUtil.changeBase64(imgAddress);
                r.setBase64(base64);
            }
            return RestMessage.success(list);
        } else {
            Page<LeaveAbsenceDto> page = new Page<>();
            page.setCurrent(leaveAbsenceDto.getPageNo());
            page.setSize(leaveAbsenceDto.getPageSize());
            IPage<LeaveAbsenceDto> list = leaveAbsenceMapper.selectLeaveAbsence(leaveAbsenceDto, page);
            for(LeaveAbsenceDto r : list.getRecords()){
                String imgAddress = r.getImgAddress();
                if(imgAddress != null && !"".equals(imgAddress)){
                    String base64 = FileUploadUtil.changeBase64(imgAddress);
                    r.setBase64(base64);
                }
            }
            return RestMessage.success(list);
        }
    }
}
